package com.scalar.chiptrack.operations.dataentry.actionCommands;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.dataentry.forms.DieBankBinningInfoForm;
import com.scalar.chiptrack.operations.dataentry.DataEntryHelper;
import com.scalar.chiptrack.operations.dataentry.DieBankBinningInfoWafer;
import com.scalar.chiptrack.operations.dataentry.dao.DieBankBinningInfoDAO;
import com.scalar.chiptrack.operations.dataentry.dao.BinQuantity;
import com.scalar.chiptrack.operations.process.dao.LotDAO;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * User: Murali Krishna Yengala
 * Date: Jul 22, 2005
 * Time: 6:33:41 PM
 */
public class DieBankBinningInfoLoadWafersCommand extends ChipTrackExtendedAction{
    public ActionForward executeRequest( ActionMapping mapping,
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response) throws Exception
    {
        HashMap errors              = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
        HashMap info                = ( HashMap ) request.getAttribute( INFO_MESSAGES );
        String forward = "dieBankBinningInfoEntry";
        DieBankBinningInfoForm dieBankBinningInfoForm = (DieBankBinningInfoForm) form;

        try
        {
            int processStepId = Integer.parseInt( dieBankBinningInfoForm.getProcessStepId() );
            int locationId = Integer.parseInt( dieBankBinningInfoForm.getLocationId());
            int lotId = Integer.parseInt( dieBankBinningInfoForm.getLotId());
            int goodBinsCount = 1;
            DataEntryHelper.loadSortPartNumbers( request);
            DataEntryHelper.loadLocations( processStepId, request);
            DataEntryHelper.loadSortLots( processStepId, locationId, request);

            ArrayList noOfBins = LotDAO.getTotalNoOfBins( ""+processStepId );
            String goodBins = (String) noOfBins.get(0);
            goodBinsCount = Integer.parseInt(goodBins);
            dieBankBinningInfoForm.setGoodBinsCount( goodBinsCount );

            ArrayList wafersList = DieBankBinningInfoDAO.loadWafersForSortLot( lotId, goodBinsCount);
            if( wafersList != null && wafersList.size() > 0)
            {
                dieBankBinningInfoForm.setWafers(wafersList);
            }
            else
            {
                wafersList = null;
                wafersList = new ArrayList();
                ArrayList binQuantities = new ArrayList();
                for( int i =0; i < goodBinsCount; i++ )
                {
                    BinQuantity binQuantity = new BinQuantity();
                    binQuantity.setBinQuantity( "");
                    binQuantities.add( binQuantity);
                }
                wafersList.add( new DieBankBinningInfoWafer("1", "", 0.0f, binQuantities, true, false, true));
                dieBankBinningInfoForm.setWafers(wafersList);
                info.put("No Wafers were found for this Lot", null);
            }
            saveToken( request );
        }
        catch( Exception e)
        {
            e.printStackTrace();
            errors.put(ERROR_INTERNAL_SERVER_ERROR, "Could not load Lot Information");
            DataEntryHelper.loadSortPartNumbers( request);
            DataEntryHelper.resetForm( dieBankBinningInfoForm);
        }
        request.setAttribute (SessionKeys.INFO_MESSAGES, info);
        request.setAttribute (SessionKeys.ERROR_MESSAGES, errors);
        return mapping.findForward( forward );
    }
}
